package server.business.controller;

//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import server.business.controller.dto.bagRecycle;
import server.business.mapper.BloodBagRecycleMapper;
import server.business.pojo.BloodBagRecycle;
import server.business.service.BloodBagRecycleService;
import server.business.service.BloodInformationService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * @author wwjh
 * @date 2022年08月05日 10:57
 */
@Slf4j
@RestController
@RequestMapping("/recycle")
public class BloodBagRecycleController {
    @Autowired
    private BloodBagRecycleService bloodBagRecycleService;
    @Autowired
    private BloodBagRecycleMapper bloodBagRecycleMapper;

    @Autowired
    private BloodInformationService bloodInformationService;

    @GetMapping("/getAll")
    public Result getAll(){
        List<BloodBagRecycle> bloodBagRecycles = bloodBagRecycleService.getAll();
        return Result.success(bloodBagRecycles);
    }
    @PostMapping("/normal/Data")
    @ResponseBody
    public boolean requestdata1(@RequestBody bagRecycle bagRecycle){
        if (StrUtil.isBlank(bagRecycle.getBloodId())||StrUtil.isBlank(bagRecycle.getBloodLocation())||StrUtil.isBlank(bagRecycle.getRecyclePeople())||StrUtil.isBlank(bagRecycle.getRecyclePeopleNumber())){
            return false;
        }else{
            log.info("-------------------------------");
            BloodBagRecycle bloodBagRecycle =new BloodBagRecycle();
            bloodBagRecycle.setRecyclePeople(bagRecycle.getRecyclePeople());
            bloodBagRecycle.setRecyclePeopleNumber(bagRecycle.getRecyclePeopleNumber());

            GenerateNumberBySnowFlake blood_recycle_id = new GenerateNumberBySnowFlake(bagRecycle.getRecyclePeopleNumber(),"RC");
//            log.info("--------------");
//            System.out.println(blood_recycle_id.genNumberByflag());
//            log.info("--------------");
//            log.info(String.valueOf(blood_recycle_id));
            bloodBagRecycle.setBloodRecycleId(blood_recycle_id.genNumberByflag());
            bloodBagRecycle.setNumber(Long.valueOf(bagRecycle.getBloodId()));
            bloodBagRecycle.setLocation(bagRecycle.getBloodLocation());
            bloodBagRecycle.setRecycleTime(new Date());
            bloodInformationService.setStateByBloodNumber(bagRecycle.getBloodId(),"15");
            bloodBagRecycle.setState("02");
            bloodBagRecycle.setIsDeleted(0);
            bloodBagRecycleService.saveOrUpdate(bloodBagRecycle);
            log.info(String.valueOf(bloodBagRecycle));
            return true;
        }

    }
    @GetMapping("/normal/page")
    @ResponseBody
    public IPage<BloodBagRecycle> findPage(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String blood_recycle_id= request.getParameter("blood_recycle_id");
        String blood_id = request.getParameter("blood_id");
        log.info("-------------------");
        log.info(blood_id);
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String recycle_people = request.getParameter("recyclePeople");
        String state = request.getParameter("state");
        IPage<BloodBagRecycle> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodBagRecycle> queryWrapper = new QueryWrapper<>();
        //如果回收编号被填写
        if (blood_recycle_id != "" && blood_recycle_id !=null) {
            queryWrapper.like("blood_recycle_id", blood_recycle_id);
        }
        //如果血袋编码被填写
        if (blood_id != "" && blood_id!=null) {
            log.info("-------------------");
            log.info(blood_id);
            queryWrapper.eq("number", blood_id);
        }
        if (state != "" && state !=null){
            queryWrapper.eq("state",state);
        }
        if (recycle_people != "" && recycle_people !=null) {
            queryWrapper.eq("recycle_people", recycle_people);
        }
        if (startTime != "" && startTime !=null){
            queryWrapper.ge("recycle_time",startTime);
        }
        if (endTime != ""&& endTime !=null){
            queryWrapper.le("recycle_time",endTime);
        }
        return bloodBagRecycleService.page(page,queryWrapper);

    }
    //分页总查询
    @GetMapping("/normal/page1")
    public IPage<BloodBagRecycle> findPage1(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "1") Integer pageSize){
        IPage<BloodBagRecycle> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodBagRecycle> queryWrapper = new QueryWrapper<>();
        //总查询
        return bloodBagRecycleService.page(page,queryWrapper);

    }
}
